package com.wyh.controller.g3controller;

import com.wyh.common.result.Result;
import com.wyh.domain.po.g3po.Charging;
import com.wyh.domain.po.g3po.G3Contract;
import com.wyh.domain.vo.g3vo.G3BillDetailsVO;
import com.wyh.domain.vo.g3vo.BillVO;
import com.wyh.domain.vo.g3vo.BillingResultVO;
import com.wyh.domain.vo.g3vo.DifferentialAccountVO;
import com.wyh.mapper.g3mapper.ContractItemMapper;
import com.wyh.mapper.g3mapper.G3ContractMapper;
import com.wyh.service.g3service.BillingService;
import com.wyh.service.g3service.ChargingService;
import com.wyh.service.g3service.ContractService;
import com.wyh.service.g3service.GenerateBillingContent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * BillController 控制器，处理账单相关操作
 */
@Api(tags = "入账")
@RestController
@RequestMapping("/charging")
@Component("g3BillController")
public class BillController {

    @Autowired
    BillingService billingService;

    /**
     * Created with IntelliJ IDEA.
     *
     * @Author: cc
     * @Date: 2024/06/05/9:26
     **/
    @Autowired
    GenerateBillingContent generateBillingContent;
    @Autowired
    G3ContractMapper g3ContractMapper;
    @Autowired
    ContractService g3ContractService;
    @Autowired
    ContractItemMapper contractItemMapper;
    @Autowired
    ContractService contractService;
    @Autowired
    ChargingService chargingService;

     @PostMapping("/generateBill")
     public List<Charging> generateBill(){
         List<G3Contract> chargingList=contractService.getAllContracts();
         List<Charging> result=new ArrayList<>();
         for (G3Contract contract:chargingList) {
             List<Charging> chargingList1=generateBillingContent.GenerateBilling(contract);
             for (Charging charging:chargingList1) {
                 result.add(charging);
                 chargingService.addCharging(charging);
             }
         }
         return result;
     }


    /**
     * 手动入账
     */
    @ApiOperation("手动入账")
    @PostMapping("/manual")
    public Result<BillingResultVO> manualBilling(@RequestBody Map<String, List<Integer>> payload) {
        List<Integer> recordIds = payload.get("recordIds");
        Result<Map<String, Object>> result = billingService.manualBilling(recordIds);
        BillingResultVO vo = convertToBillingResultVO(result);
        return Result.success(vo);
    }

    /**
     * 按合同入账
     */
    @ApiOperation("按合同入账")
    @PostMapping("/byContract")
    public Result<BillingResultVO> billingByContract(@RequestBody Map<String, List<Integer>> request) {
        List<Integer> recordIds = request.get("recordIds");
        Result<Map<String, Object>> result = billingService.billingByContract(recordIds);
        BillingResultVO vo = convertToBillingResultVO(result);
        return Result.success(vo);
    }


    /**
     * 全部按合同入账
     */
    @ApiOperation("全部按合同入账")
    @PostMapping("/allByContract")
    @Transactional
    public Result<BillingResultVO> billingAllByContract(RedirectAttributes redirectAttributes) {
        Result<Map<String, Object>> result = billingService.billingAllByContract();
        redirectAttributes.addFlashAttribute("message", result.getMsg());
        BillingResultVO vo = convertToBillingResultVO(result);
        return Result.success(vo);
    }

    private BillingResultVO convertToBillingResultVO(Result<Map<String, Object>> result) {
        // 检查 result 是否为 null
        if (result == null) {
            throw new IllegalArgumentException("Result cannot be null");
        }

        // 初始化 BillingResultVO
        BillingResultVO vo = new BillingResultVO();
        vo.setMessage(result.getMsg());

        // 获取 data 并进行 null 检查
        Map<String, Object> data = result.getData();
        if (data == null) {
            data = Collections.emptyMap();
        }

        // 使用 getOrDefault 获取 notFoundIds、failedIds、successIds
        vo.setNotFoundIds((List<Integer>) data.getOrDefault("notFoundIds", Collections.emptyList()));
        vo.setFailedIds((List<Integer>) data.getOrDefault("failedIds", Collections.emptyList()));
        vo.setSuccessIds((List<Integer>) data.getOrDefault("successIds", Collections.emptyList()));

        // 获取 billData, billDetailsData 和 differentialAccountData
        vo.setBillData((List<BillVO>) data.getOrDefault("billData", Collections.emptyList()));
        vo.setBillDetailsData((List<G3BillDetailsVO>) data.getOrDefault("billDetailsData", Collections.emptyList()));
        vo.setDifferentialAccountData((List<DifferentialAccountVO>) data.getOrDefault("differentialAccountData", Collections.emptyList()));

        return vo;
    }

}
